package com.ctg.itrdc.imageconsole.control.ssh.task;

import java.sql.Connection;
import java.util.List;

import javax.websocket.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ctg.itrdc.imageconsole.control.ssh.model.SessionOutput;
import com.ctg.itrdc.imageconsole.control.ssh.model.User;
import com.ctg.itrdc.imageconsole.control.ssh.utils.DBUtils;
import com.ctg.itrdc.imageconsole.control.ssh.utils.SessionOutputUtil;


/**
 * 
 * @description: 将输出的内容发送到websocket客户端
 * @version: v1.0.0 
 * @author: Piao.Wu
 * 
 * Modification History:
 * Date               Author      Version     Description
 * -----------------------------------------------------------------
 * 2016年11月17日 下午2:44:50     Piao.Wu       v1.0.0        create
 *
 */
public class SentOutputTask implements Runnable {

	private static Logger log = LoggerFactory.getLogger(SentOutputTask.class);

	Session session;
	Long sessionId;
	User user;

	public SentOutputTask(Long sessionId, Session session, User user) {
		this.sessionId = sessionId;
		this.session = session;
		this.user = user;
	}

	public void run() {
//		Gson gson = new Gson();
		Connection con = DBUtils.getConn();
		while (session.isOpen()) {
			List<SessionOutput> outputList = SessionOutputUtil.getOutput(con, sessionId);
			try {
				if (outputList != null && !outputList.isEmpty()) {
//					String json = gson.toJson(outputList);
//					System.out.println(json);
//					this.session.getBasicRemote().sendText(json);
					System.out.println(outputList.get(0).getOutput().toString());
					this.session.getBasicRemote().sendText(outputList.get(0).getOutput().toString());
				}
				Thread.sleep(50);
			} catch (Exception ex) {
				log.error(ex.toString(), ex);
			}
		}
		DBUtils.closeConn(con);
	}
}
